package com.xunyji.demo03.realm.usedemo;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

/**
 * @author AltEnter
 * @create 2019-01-20 21:15
 * @desc SimpleAccountRealm使用Demo类
 **/
public class SimpleAccountRealmDemo {

    private SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();

    @Before
    public void addUser() {
        simpleAccountRealm.addAccount("fury", "111111");
    }

    @Test
    public void test01() {
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(simpleAccountRealm);

        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("fury", "111111");

        subject.login(token);

        System.out.println(String.format("认证结果为：%s", subject.isAuthenticated()));

        subject.logout();

        System.out.println(String.format("认证结果为：%s", subject.isAuthenticated()));


    }
}

